package com.tang.studentinformationmanagementsystem.service.Impl;


import com.tang.studentinformationmanagementsystem.exception.DepartmentNotEmptyException;
import com.tang.studentinformationmanagementsystem.mapper.DeptMapper;
import com.tang.studentinformationmanagementsystem.pojo.Dept;
import com.tang.studentinformationmanagementsystem.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

//    @Autowired
//    private EmpMapper empMapper;
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public void delete(Integer id) {
        Integer num = deptMapper.coutEmpByDeptId(id);
        log.info("该部门下还有员工，不能删除,",num);
        if (num > 0) {
            throw new DepartmentNotEmptyException("该部门下还有员工，不能删除");
        }else {
            deptMapper.deleteById(id);
        }

    }

    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now().toString());
        dept.setUpdateTime(LocalDateTime.now().toString());
        deptMapper.add(dept);
    }

    @Override
    public Dept getById(Integer id) {
        if (id != null) {
            return deptMapper.getById(id);
        }
        return null;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now().toString());
        deptMapper.updata(dept);
    }
}
